//
// const sss = require('shamirs-secret-sharing')
// var secret = "123"
// // const secrets = Buffer.from(secret)
// const shares = sss.split(secret, { shares: 3, threshold: 2 })
// console.log("shares1:", shares[0].toString("hex"))
// console.log("shares2:", shares[1].toString("hex"))
// console.log("shares3:", shares[2].toString("base64"))
// var s1 = "CAyOqh0LbvIdsgBYgjiE0TngO4H6y1oU0LIIIGorpPGes=K1FE21YowfC2x0k14E0IWBZ_OIzfKr4P3rvFq2xfRFQ="
// var s2 = "CA31ivrMxeQSu2o8DZRCLm7aKDUmG1RiYJdi2DFl91i7w=Nywcqf3ZY9zFsYj8ID7VlPU102hZ5S-kVlWHgQl9T0k="
// var s3 = "CAGdXEEB_C1Smde21QW9E1bqABysr2dGi7pcZQ1vPQ-ds=ARS1HSIz88cYCLFGHbGwElZSKl82tR97JZLQm8j1cS0="
// var shares_go = [Buffer.from(s1), Buffer.from(s2), Buffer.from(s3)]
// // const recovered = sss.combine(shares.slice(0, 2))
// const recovered = sss.combine(shares)
// console.log(recovered.toString()) // 'secret key'

const { split, join } = require('shamir');
const { randomBytes } = require('crypto');

// the total number of shares
const PARTS = 3;
// the minimum required to recover
const QUORUM = 2;
// you can use any polyfill to covert between string and Uint8Array
const utf8Encoder = new TextEncoder();
const utf8Decoder = new TextDecoder();


const secret = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgPbk7uX+BhJALxTTzDUAqk1/6+WBletfw69Yf7rf3Fz+gCgYIKoEcz1UBgi2hRANCAARcf6564lLpwHQrLq7S5KIalJTP3MYsLEavUzCagCb14esWIaj0cfek9m7OdCtcdyXSRdovdQbLMBHFoV7D4rMb";;
const secretBytes = utf8Encoder.encode(secret);
// parts is a object whos keys are the part number and
// values are shares of type Uint8Array
const fromHexString = hexString =>
    new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));

var values = []
values[0] = new Uint8Array(fromHexString("683ee2bfecfac997a065cf62c858824f00c0c43693868983c26fe6f0f76ff5b64ae471de79902563d0a535bae90872891ac2d709a14e2215cf162380fbc848743c13a6a177c4e5141e6b7b41424a68f92f299365d576959237390f578229bce426d743d48930190f273e1f6bc9caccc453710b6a767a01a0ee7331f295f80e99c426e46b2d7e731427fd41016dade639967d2cb46f48145d1dbe79785788931695b7e58082e0e9ea2f6c3b73fd9c0fefe9096c698352bb5e7a167cc31713936b78a4f0b1829913d9"))
values[1] = new Uint8Array(fromHexString("07a71699004646f68c0c520d4d3b8c57f54ccf27febec6d45a1b443d2d09ba8d5738393b34e3f75f17c8b1a90ae317a0c439084bc67439ec3df285cf65776c66b4d994ca53c0a072412f30362b087a700419d660e87a9b6aa4fc6307dafb8808910449168733cdd8e7c7686e7f4c51566521e07146ae5d019b52b66fa833efbf47df8a2305213d8bfa3f7ef28f963ce7e335fbe9211eed1993a254a3f2a4a8d5ead66f4bba716672156f2f374de6826065993027eb08dc2658df5e49f9e0e51c41a9a2b543bff10f"))
values[2] = new Uint8Array(fromHexString("22d0b372addbca20612bd028c71a7f5fa6c13f282c5f0a10db37d38f922b766d5c8501910f3bb94ba31acd51a2ba344e8e99b475126230bb9aaee703e6eb7068cc9f731a4f356a507413091b0c3674fe1d091c630a7e68cbd5bf47371bb56da5fcbc4fa174328195a790456de4c7d3d17711b97856e26997414dcbed4a83b054cf88591b1d1407feb1886ba3d17683ad390db62b1b2cba25e95f4fea914948943600e0fb5bf7eaf3036e230bd439f9ece8e9041d3a3e080e469840c6a3b13e31565b6540f554afb4"))
const parts = {};

// eslint-disable-next-line no-plusplus
for (let i = 0; i < 3; i++) {
    const part = `${i + 1}`;
    parts[part] = values[i];
}


// const parts = split(randomBytes, PARTS, QUORUM, secretBytes);
console.log(parts)
// we only need QUORUM parts to recover the secret
// to prove this we will delete two parts
// parts[1] = new Uint8Array([207, 186, 102])
// parts[2] = new Uint8Array([176, 254, 193])
// console.log(parts)
// delete parts[3];
// console.log(parts)

// we can join three parts to recover the original Unit8Array
const recovered = join(parts);
// prints 'hello there'
console.log(utf8Decoder.decode(recovered) === secret);